package com.igeek.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.app.spring.SpringAppEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * 一共有两个配置类，一个是ProcessEngineConfiguration,一个是SpringAppEngineConfiguration
 * 他们里面都需要重写configure方法来进行配置
 * 配置数据源应该在SpringAppEngineConfiguration中设定
 * 前者是配置ProcessEngine的，如自动生成表,设置中文,在yml文件中配置的属性便是在此类中读取
 */
@Component
@PropertySource("classpath:application.properties")
@Configuration
@Slf4j
public class DatasourceConfigurator  implements EngineConfigurationConfigurer<SpringAppEngineConfiguration>  {

    @Value("${flow.datasource.url}")
    private String url;

//    @Value("${flow.datasource.type}")
//    private Class<? extends DataSource> type;

    @Value("${flow.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${flow.datasource.username}")
    private String username;

    @Value("${flow.datasource.password}")
    private String password;


    //配置数据源
    @Bean
    public DruidDataSource flowDataSource() throws PropertyVetoException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    /**
     * @return
     * @throws PropertyVetoException
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.druid.datasource")
    public DruidDataSource dataSource() throws PropertyVetoException {
        return new DruidDataSource();
    }

    //配置第一个数据源的事务管理器，定义bean名称为 transactionManagerOne
    @Primary
    @Bean(name = "transactionManagerOne")
    public PlatformTransactionManager transactionManagerOne(@Qualifier("dataSource") DataSource dataSourceOne) {
        return new DataSourceTransactionManager(dataSourceOne);
    }

    //flowable 事物管理器
    @Bean(name = "transactionManagerTwo")
    public PlatformTransactionManager transactionManagerTwo() throws PropertyVetoException {
        return new DataSourceTransactionManager(flowDataSource());
    }

    @Override
    public void configure(SpringAppEngineConfiguration engineConfiguration) {
        try {
            //把数据源设置进来
            engineConfiguration.setDataSource(flowDataSource());
            //配置对应的事务管理器
            engineConfiguration.setTransactionManager(transactionManagerTwo());
            //log.info("配置flowable数据源成功");
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }

}